package com.youtills.core.admin.db.dao;

import com.youtills.core.admin.IAdminDao;
import com.youtills.core.admin.ILookup;
import com.youtills.core.admin.ILookupType;
import com.youtills.core.admin.LookupType;
import com.youtills.core.db.DataSourceType;
import com.youtills.core.mybatis.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ravi
 * Date: 7/23/12
 * Time: 11:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class AdminDao implements IAdminDao {

    private final SqlSessionFactory sqlSessionFactory;

    private static final AdminDao instance = new AdminDao();

    /**
     *
     */
    private AdminDao() {

        String resource = "com/youtills/core/admin/db/dao/mybatis-config.xml";
        sqlSessionFactory = MybatisSqlSessionFactoryBuilder.buildSessionFactory(resource, DataSourceType.QGLDB);

    }

    public static AdminDao getInstance() {
        return instance;
    }

    private Map<Long, List<ILookup>> lookupsByType;
    private Map<Long, Map<Long, ILookup>> lookupsByTypeAndId;


        public List<ILookup> getLookups(ILookupType lookupType) {
        if(lookupsByType == null){
            loadLookups();
        }
        if(lookupsByType != null){
            return lookupsByType.get(lookupType.getType());
        }
        return null;
    }

    public ILookup getLookup(ILookupType lookupType, Long lookupId) {
        if(lookupsByTypeAndId == null){
            loadLookups();
        }
        if(lookupsByTypeAndId != null){
            Map<Long, ILookup> lookups = lookupsByTypeAndId.get(lookupType.getType());
            if(lookups != null){
                return lookups.get(lookupId);
            }
        }
        return null;
    }

    public synchronized void loadLookups(){

        if(this.lookupsByType == null){

            List<Lookup> _lookups = null;
            SqlSession session = sqlSessionFactory.openSession();
            try{
                _lookups = session.selectList("com.youtills.core.admin.db.dao.get-lookups");
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                session.close();
            }

            if(_lookups != null){
                Map<Long, List<ILookup>> _lookupsByType = new HashMap<Long, List<ILookup>>();
                Map<Long, Map<Long, ILookup>> _lookupsByTypeAndId = new HashMap<Long, Map<Long, ILookup>>();
                for(Lookup lookup : _lookups){
                    Long id = lookup.getId();
                    Long type = lookup.getType();

                    List<ILookup> lookups = _lookupsByType.get(type);
                    if(lookups == null){
                        lookups = new ArrayList<ILookup>();
                        _lookupsByType.put(type, lookups);
                    }
                    lookups.add(lookup);

                    Map<Long, ILookup> lookupByTypeAndId = _lookupsByTypeAndId.get(type);
                    if(lookupByTypeAndId == null){
                        lookupByTypeAndId = new HashMap<Long, ILookup>();
                        _lookupsByTypeAndId.put(type, lookupByTypeAndId);
                    }
                    lookupByTypeAndId.put(id, lookup);

                }
                this.lookupsByType = _lookupsByType;
                this.lookupsByTypeAndId = _lookupsByTypeAndId;
            }

        }

    }
}
